﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERF;
using Microsoft.Xna.Framework.Graphics;

namespace FirstPerson {
    /// <summary>
    /// Represents a shader model 2.0 effect that is used for rendering terrain.
    /// </summary>
    class BasicTerrainShader : EffectBase {
        private EffectParameter shaderIndexParameter;
        private EffectParameter groundTextureParameter;
        private EffectParameter mountainTextureParameter;
        private EffectParameter mudTextureParameter;
        private EffectParameter groundAltitudeStartParameter;
        private EffectParameter groundAltitudeEndParameter;
        private EffectParameter mountainAltitudeStartParameter;
        private EffectParameter mountainAltitudeEndParameter;

        /// <summary>
        /// Creates a new instance of the BasicTerrainShader.
        /// </summary>
        /// <param name="game">The game that this instance belongs to.</param>
        public BasicTerrainShader(ERF.ERFGame game)
            : base(game.GraphicsDevice, game.Content.Load<Effect>("Effects\\BasicTerrainShader")) {

            this.InitParameters();
        }

        /// <summary>
        /// Initializes the parameter fields by fetching the corresponding shader variable parameters.
        /// </summary>
        private void InitParameters() {
            this.shaderIndexParameter = this.Parameters["PixelShaderIndex"];
            this.groundTextureParameter = this.Parameters["GroundTexture"];
            this.mountainTextureParameter = this.Parameters["MountainTexture"];
            this.mudTextureParameter = this.Parameters["MudTexture"];

            this.groundAltitudeStartParameter = this.Parameters["GroundAltitudeStart"];
            this.groundAltitudeEndParameter = this.Parameters["GroundAltitudeEnd"];
            this.mountainAltitudeStartParameter = this.Parameters["MountainAltitudeStart"];
            this.mountainAltitudeEndParameter = this.Parameters["MountainAltitudeEnd"];
        }

        /// <summary>
        /// Gets or sets the texture used for middle-altitude terrain.
        /// </summary>
        public Texture2D GroundTexture {
            get {
                return this.groundTextureParameter.GetValueTexture2D();
            }
            set {
                this.groundTextureParameter.SetValue(value);
            }
        }

        /// <summary>
        /// Gets or sets the texture used for high altitude terrain.
        /// </summary>
        public Texture2D MountainTexture {
            get {
                return this.mountainTextureParameter.GetValueTexture2D();
            }
            set {
                this.mountainTextureParameter.SetValue(value);
            }
        }

        /// <summary>
        /// Gets or sets the texture used for low altitude terrain.
        /// </summary>
        public Texture2D MudTexture {
            get {
                return this.mudTextureParameter.GetValueTexture2D();
            }
            set {
                this.mudTextureParameter.SetValue(value);
            }
        }

        /// <summary>
        /// Gets or sets the altitude, in world coordinates, where the boundary between solid mountain texture and mountain-ground texture blend starts.
        /// </summary>
        public float MountainAltitudeEnd {
            get { return this.mountainAltitudeEndParameter.GetValueSingle(); }
            set { this.mountainAltitudeEndParameter.SetValue(value); }
        }

        /// <summary>
        /// Gets or sets the altitude, in world coordinates, where the boundary between solid grass texture, and mountain-ground texture blend starts.
        /// </summary>
        public float MountainAltitudeStart {
            get { return this.mountainAltitudeStartParameter.GetValueSingle(); }
            set { this.mountainAltitudeStartParameter.SetValue(value); }
        }

        /// <summary>
        /// Gets or sets the altitude, in world coordinates, where the boundary between solid mud texture, and ground-mud texture blend starts.
        /// </summary>
        public float GroundAltitudeStart {
            get { return this.groundAltitudeStartParameter.GetValueSingle(); }
            set { this.groundAltitudeStartParameter.SetValue(value); }
        }

        /// <summary>
        /// Gets or sets the altitude, in world coordinates, where the boundary between solid grass texture, and ground-mud texture blend starts.
        /// </summary>
        public float GroundAltitudeEnd {
            get { return this.groundAltitudeEndParameter.GetValueSingle(); }
            set { this.groundAltitudeEndParameter.SetValue(value); }
        }

    }
}
